﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TuoiThoProject.DAL;
using System.Data.Linq;

namespace TuoiThoProject.BLL
{
    public class NewsBLL
    {
        NewsDataContext data = new NewsDataContext();
        #region News
        public List<tblNew> getAllNews(bool isEvent)
        {
            return (from n in data.tblNews
                    where n.isEvent == isEvent
                    select n).ToList();
        }

        public List<tblNew> getNews_ByTitle(string keyWord, bool isEvent)
        {
            return (from n in data.tblNews
                    where n.isEvent == isEvent && n.cTitle.Contains(keyWord)
                    select n).ToList();
        }

        public List<tblNew> getNews_ByKeyWord(string keyWord, bool isEvent)
        {
            return (from n in data.tblNews
                    where n.isEvent == isEvent && n.cTitle.Contains(keyWord) || n.cSummary.Contains(keyWord)
                    || n.cContent.Contains(keyWord)
                    select n).ToList();
        }

        public List<tblNew> getNews_ByPublishedDate(DateTime publishedDate, bool isEvent)
        {
            return (from n in data.tblNews
                    where n.isEvent == isEvent && n.dPublishedDate == publishedDate
                    select n).ToList();
        }

        public List<tblNew> getNews_ByCreatedDate(DateTime createdDate, bool isEvent)
        {
            return (from n in data.tblNews
                    where n.isEvent == isEvent && n.dCreatedDate == createdDate
                    select n).ToList();
        }

        public List<tblNew> getNews_ByCategory(int CategoryId, bool isEvent)
        {
            return (from n in data.tblNews
                    where n.isEvent == isEvent && n.nCategoryId == CategoryId
                    select n).ToList();
        } 
        #endregion

        public void Insert(string Title, string Summary, string Content, bool isEvent, 
            DateTime publishedDate, DateTime createdDate, int CategoryId, Binary Image)
        {
            tblNew news = new tblNew();
            news.cTitle = Title;
            news.cSummary = Summary;
            news.cContent = Content;
            news.isEvent = isEvent;
            news.dPublishedDate = publishedDate;
            news.dCreatedDate = createdDate;
            news.nCategoryId = CategoryId;
            news.Image = Image;

            data.tblNews.InsertOnSubmit(news);
            data.SubmitChanges();
        }

        public void Insert(int NewsId, string Title, string Summary, string Content, bool isEvent,
          DateTime publishedDate, DateTime createdDate, int CategoryId, Binary Image)
        {
            tblNew news = data.tblNews.SingleOrDefault(n => n.nNewsId == NewsId);

            news.cTitle = Title;
            news.cSummary = Summary;
            news.cContent = Content;
            news.isEvent = isEvent;
            news.dPublishedDate = publishedDate;
            news.dCreatedDate = createdDate;
            news.nCategoryId = CategoryId;
            news.Image = Image;

            data.SubmitChanges();
        }

        public void Delete(int NewsId)
        {
            tblNew news = data.tblNews.SingleOrDefault(n => n.nNewsId == NewsId);
            data.tblNews.DeleteOnSubmit(news);
            data.SubmitChanges();
        }
    }
}